package com.module.chat
{
	import com.connect.NetEvent;
	import com.connect.agreement.chat.ChatPacket;
	import com.connect.agreement.mclgc.ChatDataHandler;
	import com.connect.java.JavaClient;
	import com.game.performers.Actor;
	
	import flash.events.Event;
	import flash.utils.ByteArray;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	
	import org.phprpc.util.PHPSerializer;

	public class ChatManager
	{
		public static const ChannelLogin:int = 1;
		
		public static const ChannelCurrent:int= 10;
		public static const ChannelZone:int = 11;
		public static const ChannelWorld:int = 12;
		public static const ChannelTeam:int = 13;
		public static const ChannelUnion:int = 14;
		public static const ChannelPrivate:int = 17;
		
		public static const server:JavaClient = new JavaClient();
		
		public static const channelAC:ArrayCollection = new ArrayCollection([
			ChannelWorld,
			ChannelCurrent,
			ChannelZone,
			ChannelTeam,
			ChannelUnion,
			ChannelPrivate
		]);
		
		public function ChatManager()
		{
			
		}
		
		public static function connect(host:String,port:int):void
		{
			if(!(server.handler is ChatDataHandler))
				server.handler = new ChatDataHandler();
			if(!server.hasEventListener(Event.CONNECT))
				server.addEventListener(Event.CONNECT,connectHandler);
			if(!server.hasEventListener(NetEvent.SEND_PACKAGE))
				server.addEventListener(NetEvent.SEND_PACKAGE,getChatPackegeHandler);
			server.connect(host,port);
		}
		
		private static function connectHandler(event:Event):void
		{
			trace("连接到聊天服务器……");
		}
		
		private static function getChatPackegeHandler(event:NetEvent):void
		{
			var p:ChatPacket = event.data;
			switch(p.channel)
			{
				case ChatManager.ChannelLogin:
					trace("聊天服务器登录成功");
					break;
				case ChatManager.ChannelCurrent:
					
					break;
			}
		}
		
		public static var role:Actor;
		
		public static function login():void
		{
			var msg:ChatMessage = new ChatMessage();
			msg.fromrid = role.id;
			msg.fromrname = role.name;
			var p:ChatPacket = new ChatPacket(true,1,PHPSerializer.serialize(msg));
			server.sendPackage(p);
		}
		
		public static function getChannelName(data:Object):String
		{
			switch(data)
			{
				case ChannelCurrent:
					return "当前";
				case ChannelZone:
					return "区域";
				case ChannelWorld:
					return "世界";
				case ChannelTeam:
					return "组队";
				case ChannelUnion:
					return "公会";
				case ChannelPrivate:
					return "私聊";
				default:
					return "";
			}
		}
		
		public static function sendContent(content:String,channel:int):void
		{
			
		}
		
		public static function sendMsgTo(touid:int,touname:String):void
		{
			
		}
		
		public static function sendMsgToChannel(channel:int,fromrid:int,fromrname:String,content:String):void
		{
			if(channel == ChannelPrivate){
				Alert.show("��ʹ��sendMsgTo����˽����Ϣ");
				return;
			}
			var msg:ChatMessage = new ChatMessage();
			msg.content = content;
			msg.fromrid = fromrid;
			msg.fromrname = fromrname;
			var bs:ByteArray = PHPSerializer.serialize(msg);
			
		}
		
		public static function sendMsg(msg:ChatMessage,channel:int):void
		{
			var p:ChatPacket = new ChatPacket(true,channel,PHPSerializer.serialize(msg));
			
		}
	}
}